# 
# Copyright (C) 2008 X-Wrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_CONFIG_NAME:=pkg-config
PKG_CONFIG_VERSION:=0.22
PKG_CONFIG_SOURCE:=$(PKG_CONFIG_NAME)-$(PKG_CONFIG_VERSION).tar.gz
PKG_CONFIG_SITE:=http://pkgconfig.freedesktop.org/releases/
PKG_CONFIG_MD5SUM:=fd5c547e9d66ba49bc735ccb8c791f2a
PKG_CONFIG_CAT:=zcat
PKG_CONFIG_BUILD_DIR:=$(TOOL_BUILD_DIR)/$(PKG_CONFIG_NAME)-$(PKG_CONFIG_VERSION)
PKG_CONFIG_INSTALL_DIR:=$(STAGING_DIR)
PKG_CONFIG_BINARY:=$(PKG_CONFIG_NAME)
PKG_CONFIG_TARGET_PREFIX:=$(GNU_TARGET_NAME)
PKG_CONFIG_TARGET_SUFFIX:=.real
PKG_CONFIG_TARGET_PATH:=usr/bin/
PKG_CONFIG_TARGET_BINARY:=$(PKG_CONFIG_TARGET_PATH)$(PKG_CONFIG_NAME)$(PKG_CONFIG_TARGET_SUFFIX)

$(DL_DIR)/$(PKG_CONFIG_SOURCE):
	mkdir -p $(DL_DIR)
	$(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(PKG_CONFIG_SOURCE)" "$(PKG_CONFIG_MD5SUM)" "$(PKG_CONFIG_SITE)"

$(PKG_CONFIG_BUILD_DIR)/.prepare: $(DL_DIR)/$(PKG_CONFIG_SOURCE)
	mkdir -p $(TOOL_BUILD_DIR)
	$(PKG_CONFIG_CAT) $(DL_DIR)/$(PKG_CONFIG_SOURCE) | tar -C $(TOOL_BUILD_DIR) $(TAR_OPTIONS)
	touch $(PKG_CONFIG_BUILD_DIR)/.unpacked

$(PKG_CONFIG_BUILD_DIR)/.configured: $(PKG_CONFIG_BUILD_DIR)/.prepare
	(cd $(PKG_CONFIG_BUILD_DIR); rm -rf config.cache; \
		./configure \
		--prefix=/usr \
		--exec-prefix=/usr \
	);
	touch  $(PKG_CONFIG_BUILD_DIR)/.configured

$(PKG_CONFIG_BUILD_DIR)/$(PKG_CONFIG_BINARY): $(PKG_CONFIG_BUILD_DIR)/.configured
	mkdir -p $(PKG_CONFIG_INSTALL_DIR)
	$(MAKE) -C $(PKG_CONFIG_BUILD_DIR)

$(STAGING_DIR)/$(PKG_CONFIG_TARGET_BINARY): $(PKG_CONFIG_BUILD_DIR)/$(PKG_CONFIG_BINARY)
	$(CP) $(PKG_CONFIG_BUILD_DIR)/$(PKG_CONFIG_BINARY) $(STAGING_DIR)/$(PKG_CONFIG_TARGET_BINARY)
	install -m0755 ./files/pkg-config $(STAGING_DIR)/$(PKG_CONFIG_TARGET_PATH)$(PKG_CONFIG_NAME)
	ln -s $(STAGING_DIR)/$(PKG_CONFIG_TARGET_PATH)$(PKG_CONFIG_NAME) \
		$(STAGING_DIR)/$(PKG_CONFIG_TARGET_PATH)$(PKG_CONFIG_TARGET_PREFIX)-$(PKG_CONFIG_NAME)

source: $(DL_DIR)/$(PKG_CONFIG_SOURCE)
prepare: $(PKG_CONFIG_BUILD_DIR)/.prepare
compile: $(PKG_CONFIG_BUILD_DIR)/$(PKG_CONFIG_BINARY)
install: $(STAGING_DIR)/$(PKG_CONFIG_TARGET_BINARY)
clean: 
	$(MAKE) -C $(PKG_CONFIG_BUILD_DIR) DESTDIR="$(PKG_CONFIG_INSTALL_DIR)" uninstall
	rm -f $(STAGING_DIR)/$(PKG_CONFIG_TARGET_BINARY)
	rm -f $(STAGING_DIR)/$(PKG_CONFIG_TARGET_PATH)$(PKG_CONFIG_NAME)
	rm -f $(STAGING_DIR)/$(PKG_CONFIG_TARGET_PATH)$(PKG_CONFIG_TARGET_PREFIX)-$(PKG_CONFIG_NAME)
	rm -rf $(PKG_CONFIG_BUILD_DIR)

